class Coupon < ApplicationRecord
  enum state: [:pending, :usable, :overdue]

  belongs_to :shop, optional: true
  belongs_to :shop_category, optional: true

  has_many :user_coupons, dependent: :destroy
  has_many :users, through: :user_coupons
  has_many :spu_coupons, dependent: :destroy
  has_many :spus, through: :spu_coupons

  validate :start_cannot_bigger_than_end

  def start_cannot_bigger_than_end
    if start_at >= end_at
      errors.add(:start_cannot_bigger_than_end, '开始时间不能大于等于结束时间')
    end
  end
end
